
#include "fun.h"

extern struct fr_list *root_list;
extern struct m_list  *root_m_list;

int main (int argc,char **argv){

  char *dumpfile=NULL;
  char errbuf[PCAP_ERRBUF_SIZE];
  int ch;
  pcap_t *fp;
  struct fr_list *tmp_fr;
  struct m_list  *tmp_m;
  /*
   *h option prints help
   *f is for reading dump file name
   *d dumps all packets from file (case 1)
   *i start interactive console using
   *a analyze packets from file (case 2)
   *c show all connections between two MACs 
   */  
  while ((ch=getopt(argc,argv,"f:hdiac"))!=-1 )
	switch(ch){

	case 'h':{c_help();return 0;}break;
	  
	  /*Read name previciuos dumped file and store it in *dumpfile */
	case 'f':
	  {
		if(!(dumpfile=(char*)malloc(strlen(optarg)+1))){
		  fprintf(stderr,"Malloc for dumpfile failed\n");
		  perror(errbuf);
		  exit(1);
		}
		strcpy(dumpfile,optarg);
	  }
	  break;

	  /*If d option is used I have to dump all packets to console 	  */
	case 'd':

	  if(!dumpfile){
		fprintf(stderr,"You have to specify dumpfile to dump it\n");
		c_help();
		free(dumpfile);
		exit(1);
	  }
	  else{
		
		if(!(fp=pcap_open_offline(dumpfile,errbuf))){
		  fprintf(stderr,"Cannot Open file %s",dumpfile);
		  perror(errbuf);
		  free(dumpfile);
		  exit(1);
		}
		printf("Input dump file is version %x.%x: \n", pcap_major_version(fp), pcap_minor_version(fp));
		/*
		 *write to stdout packets from dumpfile
		 *Calls net_callb_r function with this parameters
		 *u_char *tmp,
		 *const struct pcap_pkthdr *header,
		 *const u_char *pkt_data
		 */
		if(-1==(pcap_loop(fp,0,net_callb_r_d,NULL))){
		  fprintf(stderr,"Error occurs when proccessing packets\n");
		  perror(errbuf);
		  exit(1);
		}
	  }
	  break;
	  
	  /*if i option is used I start console UI*/ 
	case 'i':
	  printf("Start interactive session;) for win users \n");
	  break;

	  /*if a option is used I analyze all dumped packets  */
	case 'a':
	  if(!dumpfile){
		fprintf(stderr,"You have to specify dumpfile to analyze it\n");
		c_help();
		free(dumpfile);
		exit(1);
	  }
	  else{
		if(!(fp=pcap_open_offline(dumpfile,errbuf))){
		  fprintf(stderr,"Cannot Open file %s",dumpfile);
		  perror(errbuf);
		  free(dumpfile);
		  exit(1);
		}
		printf("Input dump file is version %x.%x: \n", pcap_major_version(fp), pcap_minor_version(fp));

		/*
		 *write to stdout packets from dumpfile
		 *Calls net_callb_r function with this parameters
		 *u_char *tmp,
		 *const struct pcap_pkthdr *header,
		 *const u_char *pkt_data
		 */
		if(-1==(pcap_loop(fp,0,net_callb_r_a,NULL))){
		  fprintf(stderr,"Error occurs when proccessing packets\n");
		  perror(errbuf);
		  exit(1);
		}
		
		
	  }
	  break;
	  /*If c option is given then I want to analyze tcp conections in dumpfile*/
	case 'c':
	  {
		
		if(!dumpfile){
		  fprintf(stderr,"You have to specify dumpfile to analyze it\n");
		  c_help();
		  free(dumpfile);
		  exit(1);
		}
		else{
		  /*I open dump file */
		  if(!(fp=pcap_open_offline(dumpfile,errbuf))){
			fprintf(stderr,"Cannot Open file %s",dumpfile);
			perror(errbuf);
			free(dumpfile);
			exit(1);
		  }
		  printf("Input dump file is version %x.%x: \n", pcap_major_version(fp), pcap_minor_version(fp));
		  /*I run pcap_loop an he runs net_callb_r_c which finds all connections in this file and stores it
		   *in fr_list sructure
		   */
		  if(-1==(pcap_loop(fp,0,net_callb_r_c,NULL))){
			fprintf(stderr,"Error occurs when proccessing packets\n");
			perror(errbuf);
			exit(1);
		  }
		  /*I close dumpfile because I don't know how properly seek to dumpfile start */
		  pcap_close(fp);
		  
		  if(!(fp=pcap_open_offline(dumpfile,errbuf))){
			fprintf(stderr,"Cannot Open file %s",dumpfile);
			perror(errbuf);
			free(dumpfile);
			exit(1);
		  }

		  /*tmp_fr contains fr_element choosed by user 
			tmp_m contains m_list_element choosed by user 

			s_fr_list_id(id) search fr_list for id number
			ask_user_c(root_list) ask user to choose connection to show

			s_m_list_id(id) search m_list for id number
			ask_user_c(root_m_list) ask user to choose MAC addresses to show 
   		   */
		  do {
			if(!(tmp_m=s_m_list_id(ask_user_m(root_m_list)))){
			  if(!(tmp_fr=s_fr_list_id(ask_user_c(root_list)))){
				printf("I can't list you connectios or frames if you don't select them \n");
			  }
			}
		  }while (!tmp_m && !tmp_fr);

		  /*pcap_loop calls net_callb_s_con a pass tmp to it as parameter */
		  if (!tmp_m){
			if(-1==(pcap_loop(fp,0,net_callb_s_con,(u_char *)tmp_fr))){
			  fprintf(stderr,"Error occurs when proccessing packets\n");
			  perror(errbuf);
			  exit(1);
			}
		  }
		  else {
			if(-1==(pcap_loop(fp,0,net_callb_s_m,(u_char *)tmp_m))){
			  fprintf(stderr,"Error occurs when proccessing packets\n");
			  perror(errbuf);
			  exit(1);
			} 
		  }
		}
		
		
	  }
	  
	  break;

	default:
	  {c_help();return 0;}break;
	}
  argc-=optind;
  argv+=optind;

  exit(0);
}


